package my.peerencrypt;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

import my.peerencrypt.dao.UserDao;

public class ServerMain extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8709709281534767913L;
	
	private JTable userTtable;

	private UserDao userDao;
	
	private String[] columnNames = { "用户名", "密码", "是否注册" };
	private DefaultTableModel tableModel = new DefaultTableModel(columnNames,0);
	
	public ServerMain() {
		super("对称加密-服务器端");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// String laf = UIManager.getCrossPlatformLookAndFeelClassName();
		String laf = UIManager.getSystemLookAndFeelClassName();
		try {
			UIManager.setLookAndFeel(laf);
		} catch (UnsupportedLookAndFeelException exc) {
			System.err.println("Warning: UnsupportedLookAndFeel: " + laf);
		} catch (Exception exc) {
			System.err.println("Error loading " + laf + ": " + exc);
		}
		this.userDao = new UserDao();
		JButton queryButton = new JButton("查询");
		queryButton.setBounds(5, 5, 100, 50);
		userTtable = new JTable(tableModel);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(userTtable);
		

		queryButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				List<User> list = userDao.getAllUser();
				if(list==null || list.size()==0){
					JOptionPane.showMessageDialog(ServerMain.this,
							"没有登记的用户","提示",JOptionPane.WARNING_MESSAGE);
				}else{
					tableModel.getDataVector().clear();
					for(User u:list){
						Vector<String> rowData = new Vector<String>();
						rowData.add(u.getUserName());
						rowData.add(u.getPassword());
						rowData.add(u.isHasRegister() ? "已注册":"未注册");
						tableModel.addRow(rowData);
					}
				}

			}
		});
		
		
		Container c = getContentPane();
		GroupLayout layout = new GroupLayout(c);
		c.setLayout(layout);

		// 自动设定组件、组之间的间隙
		//layout.setAutoCreateGaps(true);
		//layout.setAutoCreateContainerGaps(true);

		// LEADING -- 左对齐 BASELINE -- 底部对齐 CENTER -- 中心对齐
		ParallelGroup hpg = layout.createParallelGroup(GroupLayout.Alignment.LEADING);
		hpg.addGroup(layout.createSequentialGroup().addComponent(queryButton)).addGroup(layout.createSequentialGroup().addComponent(scrollPane));

		ParallelGroup vpg = layout.createParallelGroup(GroupLayout.Alignment.BASELINE);
		vpg.addGroup(layout.createSequentialGroup().addComponent(queryButton))
		.addGroup(layout.createSequentialGroup().addGap(30, 30, 30).addComponent(scrollPane));

		// 水平
		layout.setHorizontalGroup(hpg);

		// 设定两个Button在水平方向一样宽
		//layout.linkSize(SwingConstants.HORIZONTAL, new Component[] {
			//	fileChooseButton, analyButton });

		// 垂直
		layout.setVerticalGroup(vpg);
		setBounds(300, 300, 500, 450);
		// setLocation(200,200);
		// pack();
		setVisible(true);
	}

	
	public static void main(String[] args) {
		new Server().start();
		new ServerMain();
	}
}
